package com.geeklan.smart.ui.main.frament;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.geeklan.base.base.BaseFragment;
import com.geeklan.base.utils.ActivityUtil;
import com.geeklan.smart.R;
import com.geeklan.smart.data.model.User;
import com.geeklan.smart.data.repository.msg.CommonRepository;
import com.geeklan.smart.data.repository.user.AccountRepository;
import com.geeklan.smart.databinding.FragmentMeBinding;
import com.geeklan.smart.databinding.FragmentPetBinding;
import com.geeklan.smart.ui.device.DeviceListActivity;
import com.geeklan.smart.ui.dialog.DelDeviceAlertDialog;
import com.geeklan.smart.ui.dialog.LoginOutAlertDialog;
import com.geeklan.smart.ui.login.LoginActivity;
import com.geeklan.smart.ui.message.MessageListActivity;
import com.geeklan.smart.ui.pet.MyPetActivity;
import com.geeklan.smart.ui.settings.AboutActivity;
import com.geeklan.smart.ui.settings.CommonProblemActivity;
import com.geeklan.smart.ui.settings.SecureActivity;
import com.geeklan.smart.ui.settings.UpdatePersonalDataActivity;
import com.geeklan.smart.ui.viewmodel.common.CommonViewModel;
import com.geeklan.smart.ui.viewmodel.common.CommonViewModelFactory;
import com.geeklan.smart.ui.viewmodel.user.UserViewModel;
import com.geeklan.smart.ui.viewmodel.user.UserViewModelFactory;
import com.geeklan.smart.utils.SharedPreferencesUtil;

public class MeFragment extends BaseFragment<FragmentMeBinding> {
    private CommonViewModel mViewModel;
    private UserViewModel userViewModel;
    @Override
    protected FragmentMeBinding initBinding() {
        return FragmentMeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        mBinding.tvNikeName.setText(SharedPreferencesUtil.getString("username",""));

        mViewModel = new ViewModelProvider(this, new CommonViewModelFactory(new CommonRepository())).get(CommonViewModel.class);

        mViewModel.getMsgListLiveData().observe(this, response -> {
            if (response.getData() != null && !response.getData().isEmpty()) {
                mBinding.redView.setVisibility(View.VISIBLE);
            }else{
                mBinding.redView.setVisibility(View.GONE);
            }
        });

        userViewModel = new ViewModelProvider(this, new UserViewModelFactory(new AccountRepository())).get(UserViewModel.class);
        userViewModel.getUserInfoLiveData().observe(this, response -> {
            dismiss();
            Glide.with(this)
                    .load(response.getHeadImgUrl() + "?x-oss-process=image/resize,h_100,w_100")
                    .error(R.drawable.img_me_nan)
                    .apply(RequestOptions.circleCropTransform())
                    .into(mBinding.ivHead);
        });

        userViewModel.getUserInfoErrorLiveData().observe(this, s -> {
            dismiss();
            Toast.makeText(mContext,s,Toast.LENGTH_SHORT).show();
        });

    }

    @Override
    public void onResume() {
        mViewModel.getMsgList(20,1,0,0);
        userViewModel.getUserInfoByToken();
        super.onResume();
    }

    @Override
    protected void initView() {
        mBinding.llAbout.setOnClickListener(v -> {
            AboutActivity.startActivity(mContext);
        });
        mBinding.llHead.setOnClickListener(v -> {
            UpdatePersonalDataActivity.startActivity(mContext);
        });
        mBinding.tvNikeName.setOnClickListener(v -> {
            UpdatePersonalDataActivity.startActivity(mContext);
        });
        mBinding.ivDevice.setOnClickListener(v -> {
            DeviceListActivity.startActivity(mContext);
        });
        mBinding.llSecure.setOnClickListener(v -> {
            SecureActivity.startActivity(mContext);
        });
        mBinding.llProblem.setOnClickListener(v -> {
            CommonProblemActivity.startActivity(mContext);
        });
        mBinding.ivPet.setOnClickListener(v -> {
            MyPetActivity.startActivity(mContext);
        });
        mBinding.tvLoginOut.setOnClickListener(v -> {
            LoginOutAlertDialog alertDialog = new LoginOutAlertDialog(mContext,getString(R.string.dialog_tips));
            alertDialog.setShowRelativeLayoutButton(v1 -> {
                SharedPreferencesUtil.clear();
                ActivityUtil.getInstance().finishAllActivity();
                LoginActivity.startLogin(mContext);
            },v1 -> alertDialog.dismiss());
            alertDialog.show();
        });
        mBinding.ivMessage.setOnClickListener(v -> {
            MessageListActivity.startActivity(mContext);
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }
}